layui.use(['table','layer','form', 'layedit','jquery'], function(){
    var table = layui.table
        ,layer = layui.layer
        ,$ = layui.jquery;

    var body = "";
    var checkedData = "";
    //判断是否有选中行，没有得话则把修改按钮置灰
    $(function(){
        //获取到选中数据
        checkedData = layui.table.checkStatus('wxGoods_table').data;
    });
    table.render({
        elem: '#wxGoods_table'
        ,url:'../regimental/FindAllRegimenttal.json'
        ,response: {
            statusName: 'code'
            ,statusCode: '100012'
            ,msgName: 'msg'
            ,countName: 'total'
            ,dataName: 'data'
        }
        ,toolbar: '#toolbar'
        ,title: '字典类型'
        ,cols: [[
            {type: 'checkbox', fixed: 'left'}
            ,{field:'regimentalId', title:'团长编号', fixed: 'left'}
            ,{field:'regimentalName', title:'团长名称', fixed: 'left'}
            ,{field:'regimentalPhone', title:'手机号', fixed: 'left'}
            ,{field:'regimentalCommunity', title:'社区名称', fixed: 'left'}
            ,{field:'detailAddress', title:'具体地址', fixed: 'left'}
            ,{field:'regimentalStatue', title:'团长状态', fixed: 'left',templet:"#statue"}
            ,{field:'createtime', title:'申请时间', templet : "<div>{{layui.util.toDateString(d.createtime, 'yyyy-MM-dd HH:mm:ss')}}</div>", fixed: 'left'}
            ,{fixed: 'right', title:'操作', toolbar: '#operations'
            }
        ]]
        ,page: true
        ,id: 'table'
        ,limit:15
        ,limits:[15,20,30,40]
        ,skin: 'line'
    });


    //打开弹出层表单
    function openForm(title,event,url1,url2,params){
        layer.open({
            type: 2,
            area:['70%', '50%'],
            fixed:true,
            anim: 2,
            offset: '20px',
            maxmin:false,
            scrollbar: false,
            title: title,
            content: ['/page/regimentaldetail.html','no'],
            end: function () {
                location.reload();
            },
            btn:['通过','驳回','取消'],
            success: function(layero, index) {
                //设置body为全局变量
                body = layer.getChildFrame('body',index);
                switch (event) {
                    case 'edit':
                        var regimentalId="";
                        $.each($.parseJSON(params),function (i,v) {
                            regimentalId=v.regimentalId;
                        })
                        var iframeWin = window[layero.find('iframe')[0]['name']];
                        iframeWin.getRegmentalInfo(regimentalId);
                        break;
                }
            },
            yes: function(index, layero){
                switch (event) {
                    case 'edit':
                        var regimentalId="";
                        var regimentalStatue="";
                        $.each($.parseJSON(params),function (i,v) {
                            regimentalId=v.regimentalId;
                            regimentalStatue = 1;
                        });
                        var iframeWin = window[layero.find('iframe')[0]['name']];
                        iframeWin.updateRegimental(regimentalId,regimentalStatue);
                        break;
                }
            }
            ,btn2: function(index, layero){
                switch (event) {
                    case 'edit':
                        var regimentalId="";
                        var regimentalStatue="";
                        $.each($.parseJSON(params),function (i,v) {
                            regimentalId=v.regimentalId;
                            regimentalStatue = 3;
                        });
                        alert("你确定要驳回吗。。")
                        var iframeWin = window[layero.find('iframe')[0]['name']];
                        iframeWin.updateRegimental(regimentalId,regimentalStatue);
                        break;
                }
            }
            ,btn3: function(index, layero){
                layer.close(index);
                return false;
            }
        });
    }

    //监听行工具事件
    table.on('tool(wxGoods_table)', function(obj){
        var data = obj.data;
        if(obj.event === 'edit'){
            var array=new Array();
            array.push(data);
            var params=JSON.stringify(array);
            openForm('待审核团长信息','edit',null,null,params);
        }
    });

});

